package com.eight.cloud.core.utils.format;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import java.io.IOException;

/**
 * 对象转换
 *
 * @ClassName: ObjectFormat
 * @Author: TXC
 * @Date: 2024-10-08 17:16
 **/
public class ObjectFormat {
    private final static ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.registerModule(new JavaTimeModule());
    }

    /**
     * 对象转字符串
     * @param object 对象
     * @return 字符串
     */
    public static String objectToStr(Object object) {
        try {
            return object == null ? null : mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对象转字节
     * @param object 对象
     * @return 字节
     */
    public static byte[] objectToByte(Object object) {
        try {
            return object == null ? null : mapper.writeValueAsBytes(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 字节转对象
     * @param object 字节
     * @return 对象
     */
    public static <T> T byteToObj(Class<T> clazz, byte... object) {
        try {
            return object == null ? null : mapper.readValue(object, clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 字节转对象
     * @param object 字节
     * @return 对象
     */
    public static <T> T byteToObj(TypeReference<T> typeReference, byte... object) {
        try {
            return object == null ? null : mapper.readValue(object, typeReference);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * json 转对象
     * @param str json字符串
     * @param clazz 对象类型
     * @return 对象
     */
    public static <T> T strToObject(String str, Class<T> clazz){
        try {
            return str == null ? null : mapper.readValue(str, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }



    /**
     * json 转对象
     * @param str json字符串
     * @param typeReference 对象类型
     * @return 对象
     */
    public static <T> T strToObject(String str, TypeReference<T> typeReference){
        try {
            return str == null ? null : mapper.readValue(str, typeReference);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对象转对象
     * @param object 对象
     * @param clazz 对象类型
     * @return 对象
     */
    public static <T> T objToObject(Object object, Class<T> clazz) {
        if (object instanceof String) {
            return strToObject((String) object, clazz);
        }
        return strToObject(objectToStr(object), clazz);
    }

    /**
     * 对象转对象
     * @param object 对象
     * @param typeReference 类型
     * @return 对象
     */
    public static <T> T objToObject(Object object, TypeReference<T> typeReference) {
        if (object instanceof String) {
            return strToObject((String) object, typeReference);
        }
        return strToObject(objectToStr(object), typeReference);
    }
}
